添加程序进程名称,检测到进程运行就会关闭该进程,同时开启做题,做题结束后方可运行!关闭程序会在后台继续检测!程序不会被强制关闭,紧急情况可在进程名内添加我爱学习杀掉自身!亲测可用
using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Text;using System.Text.RegularExpressions;using System.Windows.Forms;namespace 我爱学习{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void HideMainForm() { this.Hide(); } private void ShowMainForm() { this.Show(); this.WindowState = FormWindowState.Normal; this.Activate(); }#region 窗体关闭时最小化到托盘 private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; timer1.Start(); HideMainForm(); } #endregion private void button1_Click(object sender, EventArgs e) { listBox1.Items.Add(comboBox1.Text); comboBox1.Focus(); } private void Form1_Load(object sender, EventArgs e) { timer1.Start(); comboBox1.Items.Add("QQ"); comboBox1.Items.Add("hyxd"); } int i = 0; //检测进程是否开启 private void timer1_Tick(object sender, EventArgs e) { if(listBox1.Items.Count>0) { if (IsProcessStarted(listBox1.Items[i].ToString())) { timer1.Stop(); KillProcess(listBox1.Items[i].ToString()); MessageBox.Show("系统检测到您当前,打开了" listBox1.Items[i].ToString() "请开始做题!中间出错重新开始哦!", "系统提示:"); ShowMainForm(); timer1.Start(); button4.Enabled = true; } i ; if (i > listBox1.Items.Count || i == listBox1.Items.Count) { i = 0; } } } /// <summary> /// 关闭进程 /// </summary> /// <param name="processName">进程名</param> private void KillProcess(string processName) { Process[] myproc = Process.GetProcesses(); foreach (Process item in myproc) { if (item.ProcessName == processName) { item.Kill(); } } } /// <summary> /// 此函数用于判断某一外部进程是否打开 /// </summary> /// <param name="processName">参数为进程名</param> /// <returns>如果打开了,就返回true,没打开,就返回false</returns> private bool IsProcessStarted(string processName) { Process[] temp = Process.GetProcessesByName(processName); if (temp.Length > 0) return true; else return false; } List<string> key = new List<string>() ; //添加进程 private void button2_Click(object sender, EventArgs e) { listBox2.Items.Clear(); string selUrl=null; openFileDialog1.Filter = "文本文件|*.txt";//打开文件类型 if (openFileDialog1.ShowDialog() == DialogResult.OK) { selUrl = openFileDialog1.FileName; q = 0; } else return; string[] line = File.ReadAllLines(selUrl, Encoding.Default); //遍历第10行 //遍历所有行 for (int i = 0; i < line.Length; i ) { string str = line[i].ToString(); try { string[] bit = Regex.Split(str, "猫九先森", RegexOptions.IgnoreCase); key.Add(bit[1]); listBox2.Items.Add(bit[0]); progressBar1.Maximum = i; } catch { MessageBox.Show("分隔符有误或者问题含有分隔符!", "系统提示:"); return; } } richTextBox1.Text = listBox2.Items[q].ToString(); } int q = 0; //导入问题 private void button4_Click(object sender, EventArgs e) { q ; try { if (textBox1.Text != "" && textBox1.Text == key[q - 1].ToString()) { if (q == listBox2.Items.Count) { timer1.Stop(); MessageBox.Show("感谢您!现在您可以使用" listBox1.Items[i].ToString() "了!", "系统提示:"); q = 0; } else { timer1.Start(); } richTextBox1.Text = listBox2.Items[q].ToString(); } else { q = 0; richTextBox1.Text = listBox2.Items[q].ToString(); } progressBar1.Value = q; } catch { MessageBox.Show("系统匹配出现问题!请重新导入问题!" ); } textBox1.Text = ""; textBox1.Focus(); } private void textBox1_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode==Keys.Enter&&button4.Enabled ==true ) { q ; try { if (textBox1.Text != "" && textBox1.Text == key[q - 1].ToString()) { if (q == listBox2.Items.Count) { timer1.Stop(); MessageBox.Show("感谢您!现在您可以使用" listBox1.Items[i].ToString() "了!", "系统提示:"); q = 0; } else { timer1.Start(); } richTextBox1.Text = listBox2.Items[q].ToString(); } else { q = 0; richTextBox1.Text = listBox2.Items[q].ToString(); } progressBar1.Value = q; } catch { MessageBox.Show("系统匹配出现问题!请重新导入问题!"); } textBox1.Text = ""; textBox1.Focus(); } } private void comboBox1_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode==Keys.Enter) { listBox1.Items.Add(comboBox1.Text); comboBox1.Focus(); } } }}
评论